package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.LoginDto;
import com.project.hqshorttaxi.dto.LoginParam;
import com.project.hqshorttaxi.entity.Login;
import com.project.hqshorttaxi.mapper.LoginMapper;
import com.project.hqshorttaxi.service.LoginService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginMapper loginMapper;

    @Override
    public Login getByUserId(String userId) {
        Example example = new Example(Login.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("userId", userId);
        c.andEqualTo("deleteFlag", 0);
        List<Login> list = loginMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public Login getByToken(String token) {
        Example example = new Example(Login.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("token", token);
        c.andEqualTo("deleteFlag", 0);
        List<Login> list = loginMapper.selectByExample(example);
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public Login getById(Integer id) {
        return loginMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<LoginDto> list(LoginParam param) {
        return loginMapper.getList(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(Login entity) throws Exception {
        loginMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Login entity) throws Exception {
        loginMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<Login> getByRoleId(Integer roleId) {
        Example example = new Example(Login.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("roleId", roleId);
        c.andEqualTo("deleteFlag", 0);
        List<Login> list = loginMapper.selectByExample(example);
        return list;
    }
}
